<?php
if (! defined ( "YUEXUAN_REAL_ROOT_PATH" )) {
	exit ( "Access Denied." );
}
class flowTypeException extends Exception {
	// pass
}

/**
 * 资金流动分类
 *
 * @author tongjun
 *
 */
class tally_flowType extends base {
	private $flowTypeTableName = "#tablePre#flowType";

	// 读取资金流动分类
	public function findAllFlowType($userId, $showHide = false) {
		$where = "";
		if (! $showHide) {
			$where = " and isShow=1 ";
		}
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} {$where} " . "order by flowType asc, orderIndex asc, id asc ";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRows ( $sql );
	}

	// 按类型读取资金流动分类
	public function findFlowTypeByInout($userId, $flowType, $showHide = false) {
		$where = "";
		if (! $showHide) {
			$where = " and isShow=1 ";
		}
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} and flowType={$flowType} {$where} " . "order by orderIndex asc, id asc ";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRows ( $sql );
	}

	// 根据账号id读取资金流动分类
	public function findFlowTypeById($userId, $flowTypeId) {
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} and id={$flowTypeId}";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
	}

	// 根据账号及id列表读取资金流动分类
	public function findFlowTypeByIds($userId, $flowTypeIds) {
	    $flowTypeIds[] = -1;
	    $flowTypeIds = join(",", $flowTypeIds);
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} and id in ({$flowTypeIds})";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRows ( $sql );
	}

	// 创建资金流动分类
	public function createFlowType($userId, $name, $flowType, $orderIndex = 999, $isShow=1) {
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} and name='{$name}' and flowType={$flowType}";
		$oldRow = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		if ($oldRow) {
			throw new flowTypeException ( "此分类[".$name."]己存在" );
		}
		$defaultFlowTypeInfo = array ();
		$flowTypeInfo = array (
				"userId" => $userId,
				"flowType" => $flowType,
				"name" => $name,
				"isDefault" => 0,
				"isShow" => $isShow,
				"orderIndex" => $orderIndex
		);
		$flowTypeInfo = array_merge ( $defaultFlowTypeInfo, $flowTypeInfo );
		$this->db ( ext_dbHandle::$masterDbServer )->insert ( $this->flowTypeTableName, $flowTypeInfo );
		return $this->db(ext_dbHandle::$masterDbServer)->insert_id();
	}

	public function findOrCreateFlowTypeId($userId, $name, $flowType){
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} and name='{$name}' " . "and flowType={$flowType}";
		$oldRow = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		if ($oldRow) {
			return $oldRow["id"];
		}else{
			return $this->createFlowType($userId, $name, $flowType);
		}
	}

	// 更新资金流动分类
	public function updateFlowTypeInfo($userId, $flowTypeId, $flowTypeInfo) {
		$sql = "select * from {$this->flowTypeTableName} " . "where userId={$userId} and name='{$flowTypeInfo["name"]}' " . "and flowType={$flowTypeInfo["flowType"]} and id<>{$flowTypeId}";
		$oldRow = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		if ($oldRow) {
			throw new flowTypeException ( "此分类[".$flowTypeInfo["name"]."]己存在" );
		}
		$where = array (
				"id" => $flowTypeId,
				"userId" => $userId
		);
		$this->db ( ext_dbHandle::$masterDbServer )->update ( $this->flowTypeTableName, $flowTypeInfo, $where );
	}

	// 设置默认分类
	public function setDefaultFlowType($userId, $flowType, $flowTypeId) {
		$sql = "update {$this->flowTypeTableName} set isDefault=0 " . "where userId={$userId} and flowType={$flowType} and id<>{$flowTypeId}";
		$db = $this->db ( ext_dbHandle::$masterDbServer );
		$db->begin ();
		try {
			$db->exc ( $sql );
			$flowTypeInfo = array (
					"isDefault" => 1,
					"isShow" => 1
			);
			$where = array (
					"id" => $flowTypeId,
					"flowType" => $flowType,
					"userId" => $userId
			);
			$db->update ( $this->flowTypeTableName, $flowTypeInfo, $where );
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollback ();
			throw $e;
		}
	}

	// 删除资金流动分类
	public function removeFlowType($userId, $flowTypeId) {
		$flowClass = new tally_flow ();
		$db = $this->db ( ext_dbHandle::$masterDbServer );
		$db->begin ();
		try {
			$flowClass->removeFlowByFlowTypeId ( $userId, $flowTypeId );

			$sql = "delete from {$this->flowTypeTableName} " . "where userId={$userId} and id={$flowTypeId}";
			$db->exc ( $sql );
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollback ();
			throw $e;
		}
	}

	// 删除用户的所有资金流动分类
	public function removeFlowTypeByUserId($userId) {
		$flowTypeList = $this->findAllFlowType($userId, true);
		if($flowTypeList){
			foreach($flowTypeList as $flowType){
				$this->removeFlowType($userId, $flowType["id"]);
			}
		}
	}
}
